<!DOCTYPE html>
<html lang='zh-cn'>

<head>
  <meta charset='UTF-8'>
  <meta name='viewport' content='width=device-width, initial-scale=1.0'>
  <title>JS005-ATM案例</title>
</head>

<body>
  <script>
    let balance = 1000
    let isRunning = true
    while (isRunning) {
      let opt = +prompt(`
        欢迎使用ATM机，请选择操作：
        1. 查询余额
        2. 存款
        3. 取款
        4. 退出
      `)

      switch (opt) {
        case 1:
          alert(`您的余额为：${balance}元`)
          continue
        case 2:
          let deposit = +prompt('请输入存款金额：')
          if (deposit <= 0) {
            alert('存款金额必须大于0！')
            continue
          }
          balance += deposit
          alert(`存款成功，您的新余额为：${balance}元`)
          continue
        case 3:
          let withdraw = +prompt('请输入取款金额：')
          if (withdraw <= 0) {
            alert('取款金额必须大于0！')
            continue
          }
          if (withdraw > balance) {
            alert('余额不足！')
            continue
          }
          balance -= withdraw
          alert(`取款成功，您的新余额为：${balance}元`)
          continue
        case 4:
          isRunning = false
          alert('感谢使用ATM机，再见！')
          break
        default:
          alert('无效的选项，请重新选择！')
          continue
      }
    }
  </script>
</body>

</html>